package com.yanqu.road.server.channelserver.manager.activity;

import com.yanqu.road.dao.impl.channelcenter.ChannelActivityDaoImpl;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.CrossChannelHelper;
import com.yanqu.road.server.TempMgr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 所有活动的info
 */
public class ChannelActivityInfoMgr extends TempMgr {


    private static final Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    private static final Map<Integer, Map<Integer, List<ActivityConditionInfo>>> conditionInfoMap = new ConcurrentHashMap<>();

    private static final Map<Integer, Map<String, ActivityConfig>> configMap = new ConcurrentHashMap<>();


    public static ActivityInfo getActivityInfo(int activityId) {
        if (!activityInfoMap.containsKey(activityId)) {
            synchronized (activityInfoMap) {

                // 从跨地区中心库读
                if (!activityInfoMap.containsKey(activityId)) {
                    ActivityInfo info = new ChannelActivityDaoImpl().getActivityInfoById(activityId, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
                    if (info != null) {
                        activityInfoMap.put(activityId, info);
                    }
                }

                // 异常日志
                if (!activityInfoMap.containsKey(activityId)) {
                    getLogger().error("can not find activityInfo!!! activityId = {}", activityId);
                }
            }
        }
        return activityInfoMap.get(activityId);
    }


    private static void loadConditionInfoFromDb(int activityId) {
        synchronized (conditionInfoMap) {
            if (!conditionInfoMap.containsKey(activityId)) {
                ActivityInfo activityInfo = getActivityInfo(activityId);
                if (activityInfo != null) {
                    // 读库
                    List<ActivityConditionInfo> list = new ChannelActivityDaoImpl().getActivityConditionInfoMap(Collections.singletonList(activityId)).get(activityId);
                    // 加到map
                    Map<Integer, List<ActivityConditionInfo>> listMap = new ConcurrentHashMap<>();
                    if (list != null) {
                        for (ActivityConditionInfo conditionInfo : list) {
                            listMap.putIfAbsent(conditionInfo.getType(), new ArrayList<>());
                            listMap.get(conditionInfo.getType()).add(conditionInfo);
                        }
                    }
                    conditionInfoMap.put(activityId, listMap);
                }
            }
        }
    }

    public static List<ActivityConditionInfo> getConditionInfoList(int activityId, int conditionType) {
        if (!conditionInfoMap.containsKey(activityId)) {
            loadConditionInfoFromDb(activityId);
        }
        return conditionInfoMap.containsKey(activityId) ? conditionInfoMap.get(activityId).get(conditionType) : null;
    }


    private static void loadConfigFromDb(int activityId) {
        synchronized (configMap) {
            if (!configMap.containsKey(activityId)) {
                ActivityInfo activityInfo = getActivityInfo(activityId);
                if (activityInfo != null) {
                    // 读库
                    Map<Integer, Map<String, ActivityConfig>> mapMap = new ChannelActivityDaoImpl().getActivityConfigMap(Collections.singletonList(activityId));
                    Map<String, ActivityConfig> map = mapMap.getOrDefault(activityId, new ConcurrentHashMap<>());
                    configMap.put(activityId, map);
                }
            }
        }
    }

    public static Map<String, ActivityConfig> getConfigMap(int activityId) {
        if (!configMap.containsKey(activityId)) {
            loadConfigFromDb(activityId);
        }
        return configMap.getOrDefault(activityId, new ConcurrentHashMap<>());
    }


    public static int getChannelActivityMaxRank(int activityId, int conditionType) {
        List<ActivityConditionInfo> list = getConditionInfoList(activityId, conditionType);
        if (list != null && !list.isEmpty()) {
            return ActivityHelper.getMaxRank(list);
        }
        return CrossChannelHelper.getTempRankSize();
    }

    public static int getChannelRankPartNum(int activityId) {
        ActivityInfo info = getActivityInfo(activityId);
        if (info != null) {
            try {
                int value = Integer.valueOf(info.getParam6());
                if (value > 0) {
                    return value;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return CrossChannelHelper.getTempChannelRankPartNum();
    }

    public static Map<Integer, ActivityInfo> getActivityInfoMap() {
        return activityInfoMap;
    }

    public static Map<Integer, ActivityInfo> getActivityInfoMap(List<Integer> activityTypeList,String nowTimeStr) {
        Map<Integer, ActivityInfo> infoMap = new ChannelActivityDaoImpl().getActivityInfoByType(activityTypeList,nowTimeStr, ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        synchronized (activityInfoMap){
            for(ActivityInfo activityInfo : infoMap.values()){
                if(!activityInfoMap.containsKey(activityInfo.getActivityId())){
                    activityInfoMap.put(activityInfo.getActivityId(),activityInfo);
                }
            }
        }
        return infoMap;
    }

    /**
     * 活动在展示期间
     */
    public static boolean activityInShowTime(ActivityInfo activityInfo) {
        if(null == activityInfo){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime >= activityInfo.getBeginShowTime() && nowTime < activityInfo.getEndShowTime();
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
